﻿using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Jungle.Code.DAL;

namespace Jungle.Prodamo.Core
{
    public class Estimation
    {
        #region Fields
        private int estimId;
        private int userId;
        private int estimType; // 0 - модель, 1 - компания
        private int itemId;
        private int raiting;
        private string advant;
        private string faling;
        private string description;
        private string timeUse;
        private string buyPlace;
        private bool advice;
        private DateTime pubDate;
        #endregion

        public List<Estimation> EstimList;

        #region Propert
        public int EstimId { get { return estimId; } set { estimId = value; } }
        public int UserId { get { return userId; } set { userId = value; } }
        public int EstimType { get { return estimType; } set { estimType = value; } }
        public int ItemId { get { return itemId; } set { itemId = value; } }
        public int Raiting { get { return raiting; } set { raiting = value; } }
        public string Advant { get { return advant; } set { advant = value; } }
        public string Faling { get { return faling; } set { faling = value; } }
        public string Description { get { return description; } set { description = value; } }
        public string TimeUse { get { return timeUse; } set { timeUse = value; } }
        public string BuyPlace { get { return buyPlace; } set { buyPlace = value; } }
        public bool Advice { get { return advice; } set { advice = value; } }
        public DateTime PubDate { get { return pubDate; } set { pubDate = value; } }
        #endregion

        public void Add()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Estim_Add", connect);
                command.Parameters.Add("@EstimType", SqlDbType.Int).Value = estimType;
                command.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;
                command.Parameters.Add("@ItemId", SqlDbType.Int).Value = itemId;
                command.Parameters.Add("@Raiting", SqlDbType.Int).Value = raiting;
                command.Parameters.Add("@Advant", SqlDbType.NChar, 200).Value = advant;
                command.Parameters.Add("@Faling", SqlDbType.NChar, 200).Value = faling;
                command.Parameters.Add("@Description", SqlDbType.Text).Value = description;
                command.Parameters.Add("@TimeUse", SqlDbType.NChar, 20).Value = timeUse;
                command.Parameters.Add("@BuyPlace", SqlDbType.NChar, 50).Value = buyPlace;
                command.Parameters.Add("@Advice", SqlDbType.Bit).Value = advice;

                command.CommandType = CommandType.StoredProcedure;
                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public void Delete()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Estim_Delete", connect);
                command.Parameters.Add("@EstimId", SqlDbType.Int).Value = estimId;
                command.CommandType = CommandType.StoredProcedure;

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        public void GetById()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Estim_GetById", connect);
                SqlDataReader reader = null;
                command.Parameters.Add("@EstimId", SqlDbType.Int).Value = estimId;
                command.CommandType = CommandType.StoredProcedure;

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    estimId = (int)reader["EstimId"];
                    userId = (int)reader["UserId"];
                    estimType = (int)reader["EstimType"];
                    itemId = (int)reader["ItemId"];
                    raiting = (int)reader["Raiting"];
                    advant = reader["Advant"].ToString().Trim();
                    faling = reader["Faling"].ToString().Trim();
                    description = reader["Description"].ToString().Trim();
                    timeUse = reader["TimeUse"].ToString().Trim();
                    buyPlace = reader["BuyPlace"].ToString().Trim();
                    advice = (bool)reader["Advice"];
                    pubDate = (DateTime)reader["PubDate"];
                }
                reader.Close();
                command.Connection.Close();
            }            
        }
        public void GetCurrent()
        {
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Estim_GetCurrent", connect);
                SqlDataReader reader = null;
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@EstimType", SqlDbType.Int).Value = estimType;
                command.Parameters.Add("@ItemId", SqlDbType.Int).Value = itemId;

                EstimList = new List<Estimation>();
                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Estimation estim = new Estimation();
                    estim.estimId = (int)reader["EstimId"];
                    estim.GetById();

                    EstimList.Add(estim);
                }
                reader.Close();
                command.Connection.Close();
            }
        }

    }
}